package com.hjwoo.app.views;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.hjwoo.app.R;
import com.hjwoo.app.baseviews.BaseAppCompatActivity;

import java.util.ArrayList;
import java.util.List;

public class UpLoadMaterialShowImgActivity extends BaseAppCompatActivity {

	//创建一个activity用于在其他类中finish掉此activity
	public static Activity productDetailsShowImgActivity;
	private ViewPager mViewPager;
//	ArrayList<ZoomImageView> productDetailsbms = new ArrayList<ZoomImageView>();// 定义一个图片集合用来装商品详情的图片
	//	List<View> productDetailsbms = new ArrayList<View>();// 定义一个图片集合用来装商品详情的图片
	//用来获取商品id和当前选项的位置
	String str_index;
	int imgPosition;
	TextView showimg_number_tv;
	public static List<Bitmap> imgfcLS;
	int current = 0;

	@SuppressWarnings("unchecked")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_uploadmaterialshowimg);
		productDetailsShowImgActivity = UpLoadMaterialShowImgActivity.this;
		showimg_number_tv = (TextView)findViewById(R.id.showimg_number_tv);
		if (getIntent() != null){
			imgPosition = getIntent().getIntExtra("imgPosition",0);
		}
		imgfcLS = new ArrayList<Bitmap>();
		switch (UpLoadMaterialActivity.nameFlag){
			case 1:
				imgfcLS = UpLoadMaterialActivity.imgfcLS01;
				break;
			case 2:
				imgfcLS = UpLoadMaterialActivity.imgfcLS02;
				break;
			case 3:
				imgfcLS = UpLoadMaterialActivity.imgfcLS03;
				break;
			case 4:
				imgfcLS = UpLoadMaterialActivity.imgfcLS04;
				break;
			case 5:
				imgfcLS = UpLoadMaterialActivity.imgfcLS;
				break;
		}

		findViewById(R.id.showimg_delete_iv).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {

				switch (UpLoadMaterialActivity.nameFlag){
					case 1:
						imgfcLS = UpLoadMaterialActivity.imgfcLS01;
						UpLoadMaterialActivity.imgfcLS01.remove(current);
						UpLoadMaterialActivity.tackPictureScanGridViewAdapter01.notifyDataSetChanged();
						break;
					case 2:
						imgfcLS = UpLoadMaterialActivity.imgfcLS02;
						UpLoadMaterialActivity.imgfcLS02.remove(current);
						UpLoadMaterialActivity.tackPictureScanGridViewAdapter02.notifyDataSetChanged();
						break;
					case 3:
						imgfcLS = UpLoadMaterialActivity.imgfcLS03;
						UpLoadMaterialActivity.imgfcLS03.remove(current);
						UpLoadMaterialActivity.tackPictureScanGridViewAdapter03.notifyDataSetChanged();
						break;
					case 4:
						imgfcLS = UpLoadMaterialActivity.imgfcLS04;
						UpLoadMaterialActivity.imgfcLS04.remove(current);
						UpLoadMaterialActivity.tackPictureScanGridViewAdapter04.notifyDataSetChanged();
						break;
					case 5:
						imgfcLS = UpLoadMaterialActivity.imgfcLS;
						UpLoadMaterialActivity.imgfcLS.remove(current);
						UpLoadMaterialActivity.tackPictureScanGridViewAdapter.notifyDataSetChanged();
						break;
				}
				finish();
			}
		});
		mViewPager = (ViewPager) findViewById(R.id.showimg_vp);
		findViewById(R.id.showimg_btnBack).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				finish();
			}
		});
		setImg();
	}

	private void setImg(){
		//设置总数和当前浏览的标志
		showimg_number_tv.setText(1 + "/" + imgfcLS.size());
		mViewPager.setAdapter(new ImageAdapter(UpLoadMaterialShowImgActivity.this));
		//设置viewpager的当前显示项
		mViewPager.setCurrentItem(imgPosition);
		current = imgPosition;
		str_index = imgPosition + 1 + "/" + imgfcLS.size();
		showimg_number_tv.setText(str_index);
		//为viewpager设置监听事件
		mViewPager.setOnPageChangeListener(new MyPageChangeListener());
	}
	/**
	 * 当ViewPager中页面的状态发生改变时调用
	 *
	 * @author Administrator
	 *
	 */
	private class MyPageChangeListener implements OnPageChangeListener {
		/**
		 * 当新页面被选择是调用这个页面 position: Position index of the new
		 *
		 * selected page.
		 */
		public void onPageSelected(int position) {
			current = position;
			str_index = position + 1 + "/" + imgfcLS.size();
			showimg_number_tv.setText(str_index);
		}

		public void onPageScrollStateChanged(int arg0) {

		}

		public void onPageScrolled(int arg0, float arg1, int arg2) {

		}
	}

	private class ImageAdapter extends PagerAdapter {

//		private static final String[] IMAGE_URLS = Constants.IMAGES;

		private LayoutInflater inflater;
//		private DisplayImageOptions options;

		ImageAdapter(Context context) {
			inflater = LayoutInflater.from(context);
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			container.removeView((View) object);
		}

		@Override
		public int getCount() {
			return imgfcLS.size();
		}

		@Override
		public Object instantiateItem(ViewGroup view, int position) {
			View imageLayout = inflater.inflate(R.layout.item_pager_image, view, false);
			assert imageLayout != null;
			ImageView imageView = (ImageView) imageLayout.findViewById(R.id.image);
			imageView.setImageBitmap(imgfcLS.get(position));
			view.addView(imageLayout, 0);
			return imageLayout;
		}

		@Override
		public boolean isViewFromObject(View view, Object object) {
			return view.equals(object);
		}

		@Override
		public void restoreState(Parcelable state, ClassLoader loader) {
		}

		@Override
		public Parcelable saveState() {
			return null;
		}
	}
}
